package com.sgb.jicai.viewmodel;

import android.text.TextUtils;
import androidx.lifecycle.ViewModel;

import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.JiCaiGroupBuyingRuleEntity;
import com.sgb.jicai.entity.JiCaiOfferRequireEntity;
import com.sgb.jicai.entity.JiCaiRequireEntity;
import com.sgb.jicai.view.fragment.OfferRequireFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.ArrayList;
import java.util.List;

public class OfferRequireFraModel extends ViewModel {

    private OfferRequireFragment fragment;

    /**
     * 报价规则
     * @param id
     */
    public void getOfferRequire(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getOfferRequire(id).enqueue(new ICallBack<BaseEntity<JiCaiOfferRequireEntity>>() {
            @Override
            public void onSuccess(BaseEntity<JiCaiOfferRequireEntity> data) {
                if (data == null || data.getState() == null) {
                    MToast.showToast(fragment.getContext(), "获取报价规则失败");
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    updateOfferRequireView(data.getData());
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    /**
     * 重新组装 报价规则数据
     * @param entity
     */
    public void updateOfferRequireView(JiCaiOfferRequireEntity entity) {
        //组装列表数据并展示
        List<JiCaiRequireEntity> dataList = new ArrayList<>();
        if (!TextUtils.isEmpty(entity.getManufactorCredentials())) {
            dataList.add(new JiCaiRequireEntity("供方要求", entity.getManufactorCredentials()));
        }
        if (!TextUtils.isEmpty(entity.getRegisteredCapital())) {
            dataList.add(new JiCaiRequireEntity("注册资金", entity.getRegisteredCapital()));
        }
        if (!TextUtils.isEmpty(entity.getOperatingPerformance())) {
            dataList.add(new JiCaiRequireEntity("经营业绩", entity.getOperatingPerformance()));
        }
        if (!TextUtils.isEmpty(entity.getInventoryRequirements())) {
            dataList.add(new JiCaiRequireEntity("存货要求", entity.getInventoryRequirements()));
        }
        if (!TextUtils.isEmpty(entity.getMajorCredentials())) {
            dataList.add(new JiCaiRequireEntity("专业要求", entity.getMajorCredentials()));
        }
        if (!TextUtils.isEmpty(entity.getQuailtyNum())) {
            if (!TextUtils.isEmpty(entity.getQuailtyType())&&TextUtils.equals(entity.getQuailtyType(),"2")){
                dataList.add(new JiCaiRequireEntity("质保金", entity.getQuailtyNum() + "%"));
            }else {
                dataList.add(new JiCaiRequireEntity("质保金", entity.getQuailtyNum() + "元"));
            }
        }
        if (!TextUtils.isEmpty(entity.getQuailtyCycle())) {
            dataList.add(new JiCaiRequireEntity("质保周期", entity.getQuailtyCycle() + entity.getQuailtyCycleUnit()));
        }
        if (!TextUtils.isEmpty(entity.getBillType())) {
            dataList.add(new JiCaiRequireEntity("发票类型", entity.getBillType()));
        }
        if (!TextUtils.isEmpty(entity.getTaxRate()) && !"0".equals(entity.getTaxRate())) {
            dataList.add(new JiCaiRequireEntity("税率", entity.getTaxRate()));
        }
        if (!TextUtils.isEmpty(entity.getDeliveryMethod())) {
            dataList.add(new JiCaiRequireEntity("送货方式", entity.getDeliveryMethod()));
        }
        if (!TextUtils.isEmpty(entity.getLoadType())) {
            dataList.add(new JiCaiRequireEntity("装车要求", entity.getLoadType()));
        }
        if (!TextUtils.isEmpty(entity.getUploadType())) {
            dataList.add(new JiCaiRequireEntity("卸车要求", entity.getUploadType()));
        }
        if (!TextUtils.isEmpty(entity.getQualityReq())) {
            dataList.add(new JiCaiRequireEntity("质量要求", entity.getQualityReq()));
        }
        if (!TextUtils.isEmpty(entity.getOfferOperator())) {//0否 1是
            //2 机械集采 3 设备集采
            if (!TextUtils.isEmpty(entity.getGroupbuyClass())&&entity.getGroupbuyClass().equals("2")){
                dataList.add(new JiCaiRequireEntity("是否带驾驶员", entity.getOfferOperator().equals("0")?"否":"是"));
            }else   if (!TextUtils.isEmpty(entity.getGroupbuyClass())&&entity.getGroupbuyClass().equals("3")){
                dataList.add(new JiCaiRequireEntity("是否带操作员", entity.getOfferOperator().equals("0")?"否":"是"));
            }
        }
        if (null != entity.getCustomizeOrderFields() && entity.getCustomizeOrderFields().size() > 0) {
            for (int i = 0; i < entity.getCustomizeOrderFields().size(); i++) {
                JiCaiRequireEntity jiCaiRequireEntity = entity.getCustomizeOrderFields().get(i);
                dataList.add(new JiCaiRequireEntity(jiCaiRequireEntity.getName(), jiCaiRequireEntity.getValue()));
            }
        }
        fragment.updateView(dataList);
    }

    /**
     * 集采规则
     * @param id
     */
    public void getGroupBuyingRule(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getGroupBuyingRule(id).enqueue(new ICallBack<BaseEntity<JiCaiGroupBuyingRuleEntity>>() {
            @Override
            public void onSuccess(BaseEntity<JiCaiGroupBuyingRuleEntity> data) {
                if (data == null || data.getState() == null) {
                    MToast.showToast(fragment.getContext(), "获取报价规则失败");
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    updateGroupBuyingRuleView(data.getData());
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    /**
     * 重新组装 集采规则数据
     * @param entity
     */
    private void updateGroupBuyingRuleView(JiCaiGroupBuyingRuleEntity entity) {
        //组装列表数据并展示
        List<JiCaiRequireEntity> dataList = new ArrayList<>();
        if (!TextUtils.isEmpty(entity.getStandardOfServiceCharges())) {
            dataList.add(new JiCaiRequireEntity("服务费收取", entity.getStandardOfServiceChargesString()));
            switch (entity.getStandardOfServiceCharges()){
                case "1":
                    if (!TextUtils.isEmpty(entity.getStandardOfServiceRatio())) {
                        dataList.add(new JiCaiRequireEntity("收取比例", entity.getStandardOfServiceRatio()+"%"));
                    }
                    break;
                case "2":
                    if (!TextUtils.isEmpty(entity.getStandardOfServiceCost())) {
                        dataList.add(new JiCaiRequireEntity("费用", entity.getStandardOfServiceCost()+"元"));
                    }
                    break;
                case "3":
                case "4":
                    break;
            }
        }
        if (!TextUtils.isEmpty(entity.getLoadType())) {
            dataList.add(new JiCaiRequireEntity("装车要求", entity.getLoadType()));
        }
        if (!TextUtils.isEmpty(entity.getUploadType())) {
            dataList.add(new JiCaiRequireEntity("卸车要求", entity.getUploadType()));
        }
        if (!TextUtils.isEmpty(entity.getDeliveryMethodString())) {
            dataList.add(new JiCaiRequireEntity("送货方式", entity.getDeliveryMethodString()));
            switch (entity.getDeliveryMethod()) {
                case "1": //自提
                    break;
                case "2": //供方配送
                    if (!TextUtils.isEmpty(entity.getFreeKil())) {
                        dataList.add(new JiCaiRequireEntity("免费配送距离", entity.getFreeKil()+"公里"));
                    }
                    if (!TextUtils.isEmpty(entity.getExtraRatesCost())) {
                        dataList.add(new JiCaiRequireEntity("超出收费标准", "每"+entity.getExtraRates()+"公里，费用"+entity.getExtraRatesCost()+"元/"+entity.getUnitName() ));
                    }
                    break;
                case "3": // 发起方配送
                    if (entity.getFreightRules().size() > 0) {
                        for (int i = 0; i < entity.getFreightRules().size(); i++) {
                            dataList.add(new JiCaiRequireEntity("公里数("+(i+1)+")", entity.getFreightRules().get(i).getKilometers()+"   费用(元/"+entity.getUnitName()+")   "+ entity.getFreightRules().get(i).getFreight()));
                        }
                    }
                    break;
            }
        }
        if (!TextUtils.isEmpty(entity.getQualityRequirement())) {
            dataList.add(new JiCaiRequireEntity("质量要求", entity.getQualityRequirement()));
        }
        if (!TextUtils.isEmpty(entity.getReturnRequirement())) {
            dataList.add(new JiCaiRequireEntity("退货要求", entity.getReturnRequirement()));
        }
        if (!TextUtils.isEmpty(entity.getInstallationRequirement())) {
            dataList.add(new JiCaiRequireEntity("安装要求", entity.getInstallationRequirement()));
        }
        if (!TextUtils.isEmpty(entity.getWarrantyRequirement())) {
            dataList.add(new JiCaiRequireEntity("质保要求", entity.getWarrantyRequirement()));
        }
        if (!TextUtils.isEmpty(entity.getRepairRequirement())) {
            dataList.add(new JiCaiRequireEntity("维修约定", entity.getRepairRequirement()));
        }
        if (!TextUtils.isEmpty(entity.getPaymentRequirement())) {
            dataList.add(new JiCaiRequireEntity("付款约定", entity.getPaymentRequirement()));
        }
        if (!TextUtils.isEmpty(entity.getLogisticsRequirement())) {
            dataList.add(new JiCaiRequireEntity("物流约定", entity.getLogisticsRequirement()));
        }

        if (null != entity.getGroupbuyingRuleCustomizeFields() && entity.getGroupbuyingRuleCustomizeFields().size() > 0) {
            dataList.addAll(entity.getGroupbuyingRuleCustomizeFields());
           /* for (int i = 0; i < entity.getGroupbuyingRuleCustomizeFields().size(); i++) {
                JiCaiRequireEntity jiCaiRequireEntity = entity.getGroupbuyingRuleCustomizeFields().get(i);
                dataList.add(new JiCaiRequireEntity(jiCaiRequireEntity.getName(), jiCaiRequireEntity.getValue()));
            }*/
        }
        fragment.updateView(dataList);
    }

    public void setActivity(OfferRequireFragment offerRequireFragment) {
        this.fragment = offerRequireFragment;
    }
}
